package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseCategoryTrademark;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.CategoryTrademarkVo;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName: BaseCategoryTrademarkServiceImpl
 * Package: com.atguigu.gmall.product.service.impl
 * Description:
 *
 * @Author: cqk
 * @Create: 2023/10/19 - 16:35
 * @Version: v1.0
 */
@Service
//@SuppressWarnings("all")
public class BaseCategoryTrademarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper,BaseCategoryTrademark> implements BaseCategoryTrademarkService {
    @Autowired
    private BaseCategoryTrademarkMapper baseCategoryTrademarkMapper;

    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;


    @Override
    public List<BaseTrademark> findTrademarkList(Long category3Id) {
        QueryWrapper<BaseCategoryTrademark> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("category3_id",category3Id);

        //根据三级分类id查询对应的品牌列表
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(queryWrapper);
        //处理获取品牌id
        List<Long> trademarkIdList = null;
        if(!CollectionUtils.isEmpty(baseCategoryTrademarkList)){
            //遍历后返回品牌id
            trademarkIdList = baseCategoryTrademarkList.stream().map(baseCategoryTrademark -> {

                return baseCategoryTrademark.getTrademarkId();

            }).collect(Collectors.toList());
        }

        //查询品牌数据
        List<BaseTrademark> baseTrademarkList = baseTrademarkMapper.selectBatchIds(trademarkIdList);


        return baseTrademarkList;
    }

    @Override
    public void remove(Long category3Id, Long trademarkId) {
        QueryWrapper<BaseCategoryTrademark> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category3_id",category3Id);
        queryWrapper.eq("trademark_id",trademarkId);
        baseCategoryTrademarkMapper.delete(queryWrapper);
    }

    @Override
    public List<BaseTrademark> findCurrentTrademarkList(Long category3Id) {
        List<BaseTrademark> baseTrademarkList = null;
        //1.获取当前分类已经关联的品牌id
        QueryWrapper<BaseCategoryTrademark> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category3_id",category3Id);
        //根据三级分类id查询对应的品牌列表
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(queryWrapper);
        //处理获取品牌id
        if(!CollectionUtils.isEmpty(baseCategoryTrademarkList)){
            //遍历后返回品牌id
            List<Long> trademarkIdList = baseCategoryTrademarkList.stream().map(baseCategoryTrademark -> {

                return baseCategoryTrademark.getTrademarkId();

            }).collect(Collectors.toList());

            //2.查询所有的品牌
            List<BaseTrademark> allBaseTrademarkList = baseTrademarkMapper.selectList(null);

            //3.过滤已经关联的
            baseTrademarkList = allBaseTrademarkList.stream().filter(baseTrademark -> {
                return !trademarkIdList.contains(baseTrademark.getId());
            }).collect(Collectors.toList());
        }


        return baseTrademarkList;
    }

    @Override
    public void save(CategoryTrademarkVo categoryTrademarkVo) {

        //获取品牌id
        List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();
        //遍历处理封装对象
        List<BaseCategoryTrademark> baseCategoryTrademarkList = trademarkIdList.stream().map(trademarkId -> {
            BaseCategoryTrademark categoryTrademark = new BaseCategoryTrademark();
            categoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
            categoryTrademark.setTrademarkId(trademarkId);
            return categoryTrademark;
        }).collect(Collectors.toList());

        this.saveBatch(baseCategoryTrademarkList);


    }
}
